package com.lemon.system.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import com.lemon.common.core.constant.Constants;
import com.lemon.common.security.utils.SecurityUtils;
import com.lemon.system.api.domain.BiUser;
import com.lemon.system.service.BiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import com.lemon.common.log.annotation.Log;
import com.lemon.common.log.enums.BusinessType;
import com.lemon.common.security.annotation.RequiresPermissions;
import com.lemon.system.api.domain.BiUserThirdBind;
import com.lemon.system.service.IBiUserThirdBindService;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.core.utils.poi.ExcelUtil;
import com.lemon.common.core.web.page.TableDataInfo;

/**
 * 包名称：com.lemon.system.controller
 * 类名称：BiUserThirdBindController
 * 类描述：用户与第三方系统绑定关系Controller
 * 创建人：@author lemon
 * 创建时间：2025-01-16
 */
@Api("用户与第三方系统绑定关系")
@RestController
@RequestMapping("/bind")
public class BiUserThirdBindController extends BaseController {

    @Autowired
    private IBiUserThirdBindService biUserThirdBindService;

    @Autowired
    private BiUserService userService;

    /**
     * 查询用户与第三方系统绑定关系列表
     * @Title: list
     * @Description: 查询用户与第三方系统绑定关系列表
     * @param biUserThirdBind 用户与第三方系统绑定关系
     * @return: TableDataInfo
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @GetMapping("/list")
    public TableDataInfo list(BiUserThirdBind biUserThirdBind) {
        startPage();
        List<BiUserThirdBind> list = biUserThirdBindService.selectBiUserThirdBindList(biUserThirdBind);
        return getDataTable(list);
    }

    /**
     * 导出用户与第三方系统绑定关系列表
     * @Title: export
     * @Description: 导出用户与第三方系统绑定关系列表
     * @param response 客户端响应对象
     * @param biUserThirdBind 用户与第三方系统绑定关系
     * @return:
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @RequiresPermissions("system:bind:export")
    @Log(title = "用户与第三方系统绑定关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BiUserThirdBind biUserThirdBind) {
        List<BiUserThirdBind> list = biUserThirdBindService.selectBiUserThirdBindList(biUserThirdBind);
        ExcelUtil<BiUserThirdBind> util = new ExcelUtil<BiUserThirdBind>(BiUserThirdBind.class);
        util.exportExcel(response, list, "用户与第三方系统绑定关系数据");
    }

    /**
     * 获取用户与第三方系统绑定关系详细信息
     * @Title: getInfo
     * @Description: 获取用户与第三方系统绑定关系详细信息
     * @param id 用户与第三方系统绑定关系主键
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @RequiresPermissions("system:bind:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(biUserThirdBindService.selectByPrimaryKey(id));
    }

    /**
     * 新增用户与第三方系统绑定关系
     * @Title: add
     * @Description: 新增用户与第三方系统绑定关系
     * @param biUserThirdBind 用户与第三方系统绑定关系
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @RequiresPermissions("system:bind:add")
    @Log(title = "用户与第三方系统绑定关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiUserThirdBind biUserThirdBind) {
        return toAjax(biUserThirdBindService.insert(biUserThirdBind));
    }

    /**
     * 修改用户与第三方系统绑定关系
     * @Title: edit
     * @Description: 修改用户与第三方系统绑定关系
     * @param biUserThirdBind 用户与第三方系统绑定关系
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @RequiresPermissions("system:bind:edit")
    @Log(title = "用户与第三方系统绑定关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiUserThirdBind biUserThirdBind) {
        return toAjax(biUserThirdBindService.update(biUserThirdBind));
    }

    /**
     * 删除用户与第三方系统绑定关系
     * @Title: remove
     * @Description: 删除用户与第三方系统绑定关系
     * @param ids 需要删除的用户与第三方系统绑定关系主键集合
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2025-01-16
     */
    @RequiresPermissions("system:bind:remove")
    @Log(title = "用户与第三方系统绑定关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(biUserThirdBindService.deleteByBatch(ids));
    }

    /**
     * 绑定第三方账号
     * @Title: insertThirdBind
     * @Description: 绑定第三方账号
     * @param model 当前用户x信息
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2025/1/16 10:49
     */
    @PostMapping("/insertThirdBind")
    public AjaxResult insertThirdBind(@RequestBody BiUserThirdBind model) {
        BiUser biUser = userService.selectByUuidAndSource(model.getThirdUuid(), model.getThirdSource());
        if (biUser != null) {
            if (Objects.equals(biUser.getId(), SecurityUtils.getWebUserId())) {
                return AjaxResult.error("你已经绑定当前账户，不支持重复绑定！");
            } else {
                return AjaxResult.error("绑定账户失败，当前第三方账户已经被其他用户绑定！");
            }
        } else {
            BiUserThirdBind userThirdBind = new BiUserThirdBind();
            userThirdBind.setThirdSource(model.getThirdSource());
            userThirdBind.setThirdUuid(model.getThirdUuid());
            userThirdBind.setThirdUname(model.getThirdUname());
            userThirdBind.setUserId(model.getUserId() == null ? SecurityUtils.getWebUserId() : model.getUserId());
            userThirdBind.setStatus(Constants.STR_ONE);
            biUserThirdBindService.insert(userThirdBind);
            return AjaxResult.success("恭喜你，第三方账号绑定成功！");
        }
    }

    /**
     * 解绑第三方账号
     * @Title: deleteThirdBind
     * @Description: 解绑第三方账号
     * @param model 账号信息
     * @return: AjaxResult
     * @throws: 
     * @author: huangpeng
     * @Date: 2025/1/22 17:14
     */
    @PostMapping("/deleteThirdBind")
    public AjaxResult deleteThirdBind(@RequestBody BiUserThirdBind model) {
        BiUser biUser = userService.selectByUuidAndSource(model.getThirdUuid(), model.getThirdSource());
        if (biUser != null) {
            if (Objects.equals(biUser.getId(), SecurityUtils.getWebUserId())) {
                biUserThirdBindService.deleteByUuidAndSource(model.getThirdUuid(), model.getThirdSource());
                return AjaxResult.success("解除第三方账户绑定成功！");
            } else {
                return AjaxResult.error("解除绑定失败，你未绑定当前第三方账户！");
            }
        } else {
           return AjaxResult.error("解除绑定失败，当前第三方系统账户未绑定任何本系统账户！");
        }
    }
}
